/**
 * Jaxson ajax
 *
 * Copyright Remedy Interactive, licensed under Apache version 2 license:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0.html
 *
 * This notice must remain in all java source files.
 */
package org.jaxson.struts.charts.bar;

import java.awt.Color;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jaxson.struts.charts.AbstractChartRenderer;
import org.jaxson.struts.charts.ChartFormat;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.GroupedStackedBarRenderer;
import org.jfree.chart.renderer.category.StackedBarRenderer3D;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.Dataset;

/**
 * Renderer for a Bar Chart or Stacked Bar Chart.
 * 
 * @author Joe Maisel
 *
 */
public class BarChartRenderer extends AbstractChartRenderer
{
	private static final Log lager = LogFactory.getLog( BarChartRenderer.class.getName() );
	
	/**
	 * @see org.jaxson.struts.charts.AbstractChartRenderer#render(Dataset, ChartFormat)
	 */
	@Override
	public JFreeChart render(Dataset dataset, ChartFormat format)
	{
		lager.trace(  "render"  );
		
		CategoryDataset ds = (CategoryDataset)dataset;
		BarChartFormat f = (BarChartFormat)format;
		
		JFreeChart result = null;
		
		if( f.isIs3D() )
		{
			if( f.isStacked() )
				result = ChartFactory.createStackedBarChart3D( f.getTitle(), f.getCategoryLabel(), f.getValueLabel(), ds, f.getOrientation(), f.isShowLegend(), f.isShowTooltips(), f.isShowUrls() );
			else
				result = ChartFactory.createBarChart3D( f.getTitle(), f.getCategoryLabel(), f.getValueLabel(), ds, f.getOrientation(), f.isShowLegend(), f.isShowTooltips(), f.isShowUrls() );
		}
		else
		{
			if( f.isStacked() )
				result = ChartFactory.createStackedBarChart( f.getTitle(), f.getCategoryLabel(), f.getValueLabel(), ds, f.getOrientation(), f.isShowLegend(), f.isShowTooltips(), f.isShowUrls() );
			else
				result = ChartFactory.createBarChart( f.getTitle(), f.getCategoryLabel(), f.getValueLabel(), ds, f.getOrientation(), f.isShowLegend(), f.isShowTooltips(), f.isShowUrls() );
		}
		
		if( f.getTitle() != null && f.isShowTitle() )
		{
			TextTitle tt = new TextTitle(f.getTitle());
			tt.setExpandToFitSpace( f.isExpandTitle() );
			tt.setFont( f.getTitleFont() );
		}
		
		if( f.isStacked() )
		{
			BarRenderer renderer = new GroupedStackedBarRenderer();
			if(f.isIs3D())
					renderer = new StackedBarRenderer3D();
			
			if( f.getColors() != null && f.getColors().size() > 0 )
			{
				List<Color> colors = f.getColors();

				for( int i = 0; i < colors.size(); i++ )
				{
					renderer.setSeriesPaint( i, colors.get( i ) );
				}
			}

			renderer.setBaseToolTipGenerator( f.getToolTipGenerator() );
			renderer.setBaseItemURLGenerator( f.getUrlGenerator() );
			
			CategoryPlot plot = (CategoryPlot) result.getPlot();
			plot.setRenderer( renderer );
		}
		
		 
		return result;
	}

}
